package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Menu;
import com.lostreseditores.sabersai.model.dto.MenuDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class MenuView {
    private InputText txtMenuimagenv;
    private InputText txtMenunombrev;
    private InputText txtMenupaginav;
    private InputText txtMenuidn_Menu;
    private InputText txtMenuidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<MenuDTO> data;
    private MenuDTO selectedMenu;

    public MenuView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            MenuDTO menuDTO = (MenuDTO) e.getObject();

            if (txtMenuimagenv == null) {
                txtMenuimagenv = new InputText();
            }

            txtMenuimagenv.setValue(menuDTO.getMenuimagenv());

            if (txtMenunombrev == null) {
                txtMenunombrev = new InputText();
            }

            txtMenunombrev.setValue(menuDTO.getMenunombrev());

            if (txtMenupaginav == null) {
                txtMenupaginav = new InputText();
            }

            txtMenupaginav.setValue(menuDTO.getMenupaginav());

            if (txtMenuidn_Menu == null) {
                txtMenuidn_Menu = new InputText();
            }

            txtMenuidn_Menu.setValue(menuDTO.getMenuidn_Menu());

            if (txtMenuidn == null) {
                txtMenuidn = new InputText();
            }

            txtMenuidn.setValue(menuDTO.getMenuidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtMenuimagenv != null) {
            txtMenuimagenv.setValue(null);
            txtMenuimagenv.setDisabled(true);
        }

        if (txtMenunombrev != null) {
            txtMenunombrev.setValue(null);
            txtMenunombrev.setDisabled(true);
        }

        if (txtMenupaginav != null) {
            txtMenupaginav.setValue(null);
            txtMenupaginav.setDisabled(true);
        }

        if (txtMenuidn_Menu != null) {
            txtMenuidn_Menu.setValue(null);
            txtMenuidn_Menu.setDisabled(true);
        }

        if (txtMenuidn != null) {
            txtMenuidn.setValue(null);
            txtMenuidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Menu entity = null;

        try {
            Integer menuidn = new Integer(txtMenuidn.getValue().toString());
            entity = BusinessDelegatorView.getMenu(menuidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtMenuimagenv.setDisabled(false);
            txtMenunombrev.setDisabled(false);
            txtMenupaginav.setDisabled(false);
            txtMenuidn_Menu.setDisabled(false);
            txtMenuidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtMenuimagenv.setValue(entity.getMenuimagenv());
            txtMenuimagenv.setDisabled(false);
            txtMenunombrev.setValue(entity.getMenunombrev());
            txtMenunombrev.setDisabled(false);
            txtMenupaginav.setValue(entity.getMenupaginav());
            txtMenupaginav.setDisabled(false);
            txtMenuidn_Menu.setValue(entity.getMenu().getMenuidn());
            txtMenuidn_Menu.setDisabled(false);
            txtMenuidn.setValue(entity.getMenuidn());
            txtMenuidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveMenu(FacesUtils.checkInteger(txtMenuidn),
                FacesUtils.checkString(txtMenuimagenv),
                FacesUtils.checkString(txtMenunombrev),
                FacesUtils.checkString(txtMenupaginav),
                FacesUtils.checkInteger(txtMenuidn_Menu));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteMenu(FacesUtils.checkInteger(txtMenuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateMenu(FacesUtils.checkInteger(txtMenuidn),
                FacesUtils.checkString(txtMenuimagenv),
                FacesUtils.checkString(txtMenunombrev),
                FacesUtils.checkString(txtMenupaginav),
                FacesUtils.checkInteger(txtMenuidn_Menu));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtMenuidn == null) {
                txtMenuidn = new InputText();
            }

            txtMenuidn.setValue(selectedMenu.getMenuidn());

            BusinessDelegatorView.deleteMenu(FacesUtils.checkInteger(txtMenuidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedMenu);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer menuidn, String menuimagenv,
        String menunombrev, String menupaginav, Integer menuidn_Menu)
        throws Exception {
        try {
            BusinessDelegatorView.updateMenu(menuidn, menuimagenv, menunombrev,
                menupaginav, menuidn_Menu);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("MenuView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtMenuimagenv() {
        return txtMenuimagenv;
    }

    public void setTxtMenuimagenv(InputText txtMenuimagenv) {
        this.txtMenuimagenv = txtMenuimagenv;
    }

    public InputText getTxtMenunombrev() {
        return txtMenunombrev;
    }

    public void setTxtMenunombrev(InputText txtMenunombrev) {
        this.txtMenunombrev = txtMenunombrev;
    }

    public InputText getTxtMenupaginav() {
        return txtMenupaginav;
    }

    public void setTxtMenupaginav(InputText txtMenupaginav) {
        this.txtMenupaginav = txtMenupaginav;
    }

    public InputText getTxtMenuidn_Menu() {
        return txtMenuidn_Menu;
    }

    public void setTxtMenuidn_Menu(InputText txtMenuidn_Menu) {
        this.txtMenuidn_Menu = txtMenuidn_Menu;
    }

    public InputText getTxtMenuidn() {
        return txtMenuidn;
    }

    public void setTxtMenuidn(InputText txtMenuidn) {
        this.txtMenuidn = txtMenuidn;
    }

    public List<MenuDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataMenu();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<MenuDTO> menuDTO) {
        this.data = menuDTO;
    }

    public MenuDTO getSelectedMenu() {
        return selectedMenu;
    }

    public void setSelectedMenu(MenuDTO menu) {
        this.selectedMenu = menu;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
